export interface IRect {
  x: number;
  y: number;
  w: number;
  h: number;
}
export function rect(x: number, y: number, w: number, h: number): IRect {
  return { x, y, w, h }
}
export function get_fill_distribution(num: number, zone: IRect): IRect[] {
  let row = Math.floor(Math.sqrt(num))
  let col = Math.ceil(num / row)
  let arr: number[] = []
  if (1 === num - row * (col - 1) && num > 1) {
    col -= 1
    while (num > 1) {
      arr.push(col + 1 === num ? num : col)
      num -= col
    }
  } else {
    while (num > 0) {
      arr.push(col < num ? col : num)
      num -= col
    }
  }
  let ret: IRect[] = []
  const { x: zone_x, y: zone_y, w: zone_w, h: zone_h } = zone
  let cell_h = zone_h / row
  for (let cell_y = 0; cell_y < arr.length; ++cell_y) {
    let col = arr[cell_y]
    let cell_w = zone_w / col
    for (let ix = 0; ix < col; ++ix) {
      ret.push(rect(
        zone_x + ix * cell_w,
        zone_y + cell_y * cell_h,
        cell_w,
        cell_h)
      )
    }
  }
  return ret
}

export function get_vertical_distribution(num: number, zone: IRect, cell_h: number = zone.h / num, align = 0): IRect[] {
  const all_cell_s = cell_h * num;
  const offset = (zone.h - all_cell_s) * align
  const ret: IRect[] = []
  for (let i = 0; i < num; i++) ret.push(rect(zone.x, offset + zone.y + i * cell_h, zone.w, cell_h))
  return ret
}

export function get_horizontal_distribution(num: number, zone: IRect, cell_w: number = zone.w / num, align = 0): IRect[] {
  const all_cell_s = cell_w * num;
  const offset = (zone.w - all_cell_s) * align
  const ret: IRect[] = []
  for (let i = 0; i < num; i++) ret.push(rect(offset + zone.x + i * cell_w, zone.y, cell_w, zone.h))
  return ret
}